package utils;

import com.google.common.collect.Lists;

import java.util.*;

public final class CollectionUtils {
    public CollectionUtils() {
    }

    public static <T> List<T> emptyList() {
        return Lists.newArrayList();
    }

    public static <T> int size(Collection<T> collection) {
        return Objects.isNull(collection)?0:collection.size();
    }

    public static <T> boolean isOne(Collection<T> collection) {
        return size(collection) == 1;
    }

    public static <T> boolean isEmpty(Collection<T> collection) {
        return size(collection) == 0;
    }

    public static <T> boolean isNotEmpty(Collection<T> collection) {
        return !isEmpty(collection);
    }

    public static <T> Optional<T> first(List<T> list) {
        return isNotEmpty((Collection)list)?Optional.ofNullable(list.get(0)):Optional.empty();
    }

    public static <T, S extends T> T firstEmptyDefault(Collection<T> collection, S s) {
        return isEmpty(collection)?s:collection.stream().findFirst().orElse(s);
    }

    public static <T> boolean contains(Collection<T> collection, T t) {
        return isNotEmpty(collection) && collection.contains(t);
    }

    public static <T> boolean nonContains(Collection<T> collection, T t) {
        return !contains(collection, t);
    }

    public static <T extends Number> double sum(Collection<T> numbers) {
        return isEmpty(numbers)?0.0D:numbers.stream().filter(Objects::nonNull).mapToDouble(Number::doubleValue).sum();
    }

    public static <T> boolean isEmpty(T[] arrays) {
        return Objects.isNull(arrays) || arrays.length == 0;
    }

    public static <T> boolean isNotEmpty(T[] arrays) {
        return !isEmpty(arrays);
    }

    public static <T extends Number> double sum(T[] numbers) {
        return isEmpty((Object[])numbers)?0.0D: Arrays.stream(numbers).filter(Objects::nonNull).mapToDouble(Number::doubleValue).sum();
    }

    public static <T> List<T> subListCloseOpen(List<T> list, int fromIndex, int toIndex) {
        return (List)(isEmpty((Collection)list)?Lists.newArrayList():list.subList(fromIndex, toIndex));
    }

    public static String[] toArray(Collection<String> collections) {
        if(Objects.isNull(collections)) {
            collections = Lists.newArrayList();
        }

        return (String[])((Collection)collections).toArray(new String[((Collection)collections).size()]);
    }
}
